/*
 * @Author: hongbin
 * @Date: 2022-11-25 11:28:07
 * @LastEditors: hongbin
 * @LastEditTime: 2022-11-26 00:21:06
 * @Description:
 */
import { ObjectId } from "mongodb";
import { NextApiRequest, NextApiResponse } from "next";
import { VerifyFields } from "../../../src/api/utils";
import DB from "../mongodb";
import { receiveRequest } from "../utils";

export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
    if (!receiveRequest(req, res, "POST")) return;
    // 通过socket发送申请加好友消息同意后双方均执行此请求
    console.log("请求内容", req.body);
    if (!VerifyFields.has(req.body, ["userId", "config"])) {
        return res.status(500).json("缺少关键字段");
    }

    const db = await DB;

    const update = await db.collection("partsConfig").updateOne(
        { userId: new ObjectId(req.body.userId) },
        {
            $set: {
                current: req.body.config.current,
                config1: req.body.config.config1,
                config2: req.body.config.config2,
                config3: req.body.config.config3,
            },
        }
    );

    if (update.acknowledged) {
        res.status(200).json({ data: { msg: "修改成功" } });
        return;
    }
    res.status(200).json({ data: null, msg: "修改配件配置失败", code: -1 });
}
